package com.nondo.dean.auth.service.impl;

import com.nondo.dean.auth.domain.RoleMenuExample;
import com.nondo.dean.auth.domain.RoleMenuKey;
import com.nondo.dean.auth.mapper.RoleMenuMapper;
import com.nondo.dean.auth.service.RoleMenuService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;

/**
 * @author linfeng
 */
@Service
public class RoleMenuServiceImpl implements RoleMenuService {

    @Autowired
    private RoleMenuMapper roleMenuMapper;

    @Override
    @Transactional(rollbackFor = Exception.class)
    public void saveRoleMenu(Long roleId, Long menuId) {

        RoleMenuExample example = new RoleMenuExample();
        example.createCriteria().andMenuIdEqualTo(menuId).andRoleIdEqualTo(roleId);
        List<RoleMenuKey> roleMenuKeyList = roleMenuMapper.selectByExample(example);
        if (roleMenuKeyList.isEmpty()) {
            RoleMenuKey roleMenuKey = new RoleMenuKey();
            roleMenuKey.setRoleId(roleId);
            roleMenuKey.setMenuId(menuId);
            roleMenuKey.setDelFlag(RoleMenuKey.DEL_FLAG_NORMAL);
            roleMenuMapper.insertSelective(roleMenuKey);
        }

    }

    @Override
    public RoleMenuKey findByRoleIdAndMenuId(Long roleId, Long menuId) {

        RoleMenuExample example = new RoleMenuExample();
        example.createCriteria().andRoleIdEqualTo(roleId).andMenuIdEqualTo(menuId);
        List<RoleMenuKey> roleMenuKeyList = roleMenuMapper.selectByExample(example);
        return roleMenuKeyList.isEmpty() ? null : roleMenuKeyList.get(0);
    }

    @Override
    public List<RoleMenuKey> findByMenuId(Long menuId) {
        RoleMenuExample example = new RoleMenuExample();
        example.createCriteria().andMenuIdEqualTo(menuId);
        example.setOrderByClause("menu_id asc");
        return roleMenuMapper.selectByExample(example);
    }

    @Override
    public List<RoleMenuKey> findByRoleIds(List<Long> roleIds) {
        RoleMenuExample example = new RoleMenuExample();
        example.createCriteria().andRoleIdIn(roleIds);
        example.setOrderByClause("menu_id asc");
        return roleMenuMapper.selectByExample(example);
    }

    @Override
    public List<RoleMenuKey> findByRoleId(Long roleId) {
        RoleMenuExample example = new RoleMenuExample();
        example.createCriteria().andRoleIdEqualTo(roleId);
        example.setOrderByClause("menu_id asc");
        return roleMenuMapper.selectByExample(example);
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public void deleteByRoleIdAndMenuId(Long roleId, Long menuId) {

        RoleMenuExample example = new RoleMenuExample();
        example.createCriteria().andRoleIdEqualTo(roleId).andMenuIdEqualTo(menuId);
        roleMenuMapper.deleteByExample(example);
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public void deleteByRoleId(Long roleId) {
        RoleMenuExample example = new RoleMenuExample();
        example.createCriteria().andRoleIdEqualTo(roleId);
        roleMenuMapper.deleteByExample(example);
    }

}
